using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace XZ.Logistics.WebShop
{
    public class Category : WebShopBase
    {
        private string _title = "";
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private int _importance = 0;
        public int Importance
        {
            get { return _importance; }
            private set { _importance = value; }
        }

        private string _description = "";
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        private string _imageUrl = "";
        public string ImageUrl
        {
            get { return _imageUrl; }
            set { _imageUrl = value; }
        }

        private List<Product> _allProducts = null;
        public List<Product> AllProducts
        {
            get
            {
                if (_allProducts == null)
                    _allProducts = Product.GetProducts(this.Id, "", 0, BizObject.MAXROWS);
                return _allProducts;
            }
        }

        public Category(Guid id, DateTime addedDate, string addedBy, string title, int importance, string description, string imageUrl)
        {
            this.Id = id;
            this.AddedDate = addedDate;
            this.AddedBy = addedBy;
            this.Title = title;
            this.Importance = importance;
            this.Description = description;
            this.ImageUrl = imageUrl;
        }

        public bool Delete()
        {
            bool success = Category.DeleteCategory(this.Id);
            if (success)
                this.Id = Guid.Empty;
            return success;
        }

        public bool Update()
        {
            return Category.UpdateCategory(this.Id, this.Title, this.Importance, this.Description, this.ImageUrl);
        }

        /***********************************
        * Static methods
        ************************************/

        /// <summary>
        /// Returns a collection with all the departments
        /// </summary>
        public static List<Category> GetCategories()
        {
            List<Category> categories = null;
            string key = "WebShop_Categories";

            if (WebShopBase.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                categories = (List<Category>)BizObject.Cache[key];
            }
            else
            {
                categories = WebShop.GetCategories();
                WebShopBase.CacheData(key, categories);
            }
            return categories;
        }

        /// <summary>
        /// Returns a Category object with the specified ID
        /// </summary>
        public static Category GetCategoryById(Guid categoryId)
        {
            Category category = null;
            string key = "WebShop_Category_" + categoryId.ToString();

            if (WebShopBase.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                category = (Category)BizObject.Cache[key];
            }
            else
            {
                category = WebShop.GetCategoryById(categoryId);
                WebShopBase.CacheData(key, category);
            }
            return category;
        }

        /// <summary>
        /// Updates an existing category
        /// </summary>
        public static bool UpdateCategory(Guid id, string title, int importance, string description, string imageUrl)
        {
            Category record = new Category(id, DateTime.Now, "", title, importance, description, imageUrl);
            bool ret = WebShop.UpdateCategory(record);
            BizObject.PurgeCacheItems("WebShop_Category");
            return ret;
        }

        /// <summary>
        /// Deletes an existing categroy
        /// </summary>
        public static bool DeleteCategory(Guid id)
        {
            bool ret = WebShop.DeleteCategory(id);
            //new RecordDeletedEvent("department", id, null).Raise();
            BizObject.PurgeCacheItems("WebShop_Category");
            return ret;
        }

        /// <summary>
        /// Creates a new category
        /// </summary>
        public static Guid InsertCategory(string title, int importance, string description, string imageUrl)
        {
            Category record = new Category(Guid.Empty, DateTime.Now,
               BizObject.CurrentUserName, title, importance, description, imageUrl);
            Guid ret = WebShop.InsertCategory(record);
            BizObject.PurgeCacheItems("WebShop_Category");
            return ret;
        }
    }
}